package com.boarsoft.boar.sys.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.boarsoft.bean.LogonI;
import com.boarsoft.bean.ReplyInfo;
import com.boarsoft.boar.common.Constants;
import com.boarsoft.boar.sys.SysRoleBiz;
import com.boarsoft.boar.sys.entity.SysRole;
import com.boarsoft.common.Authorized;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.web.login.Logined;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@RestController
@RequestMapping("/sys/role")
public class SysRoleAction {
	@Autowired
	private SysRoleBiz sysRoleBiz;

	@JsonIgnoreProperties({ "users", "funcs" })
	@RequestMapping("/list.do")
	@Authorized(code = "sys.role.list")
	public ReplyInfo<Object> list(@Logined LogonI<String> logon, String key, int pageNo, int pageSize, String orderBy) {
		PagedResult<SysRole> pr = sysRoleBiz.list(logon.getId(), key, pageNo, pageSize, orderBy);
		return new ReplyInfo<Object>(true, pr);
	}
	
	@RequestMapping("/save.do")
	@Authorized(code = "sys.role.save")
	public ReplyInfo<Object> save(@Logined LogonI<String> logon, SysRole o) {
		if (Util.strIsEmpty(o.getId())) {
			return this.add(logon, o);
		}
		return this.update(logon, o);
	}

	@RequestMapping("/add.do")
	@Authorized(code = "sys.role.add")
	public ReplyInfo<Object> add(@Logined LogonI<String> logon, SysRole o) {
		if (sysRoleBiz.add(o)) {
			return ReplyInfo.SUCCESS;
		}
		return Constants.REPLY_INFO_DUPLICATED;
	}

	@RequestMapping("/update.do")
	@Authorized(code = "sys.role.update")
	public ReplyInfo<Object> update(@Logined LogonI<String> logon, SysRole o) {
		if (sysRoleBiz.update(o)) {
			return ReplyInfo.SUCCESS;
		}
		return Constants.REPLY_WARN_INVALID;
	}

	@RequestMapping("/delete.do")
	@Authorized(code = "sys.role.delete")
	public ReplyInfo<Object> delete(String id) {
		sysRoleBiz.delete(id);
		return ReplyInfo.SUCCESS;
	}
}